﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TruyenCuoiViet.Source.Helper;

namespace TruyenCuoiViet.Source.UI
{
    public partial class Selectcategory : PhoneApplicationPage
    {
        public Selectcategory()
        {
            InitializeComponent();
            llsCategory.ItemsSource = App.ViewModel.Categories;
        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            TextBlock tblId = ((StackPanel)sender).FindName("tblId") as TextBlock;
            int id = int.Parse(tblId.Text);
            if(id==6)
            {
                var tb = new TextBox();
                var box = new CustomMessageBox()
                {
                    Caption = "Tìm kiếm",
                    Message = "Hãy nhập từ khóa cần tìm",
                    LeftButtonContent = "Tìm kiếm",
                    RightButtonContent = "Hủy",
                    Content = tb,
                    IsFullScreen = false
                };
                box.Dismissed += (s, e1) =>
                {
                    if (e1.Result == CustomMessageBoxResult.LeftButton)
                    {
                        var filename = tb.Text;
                        NavigationService.Navigate(new Uri("/Source/UI/MainPage.xaml?CategoryId=" + tblId.Text+"&q="+filename, UriKind.Relative));
                    }
                };
                box.Show();
                
            }
            else
            {
                NavigationService.Navigate(new Uri("/Source/UI/MainPage.xaml?CategoryId=" + tblId.Text, UriKind.Relative));
            }
            
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
            TransitionHelper.excuteTransition(phoneApplicationPage);
            base.OnNavigatingFrom(e);
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            int categoryId = int.Parse(NavigationContext.QueryString["CategoryId"]);
            NavigationService.Navigate(new Uri("/Source/UI/MainPage.xaml?CategoryId=" + categoryId, UriKind.Relative));
            base.OnBackKeyPress(e);
        }
    }
}